using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Deepcode.Flux.Core.Data;

namespace Deepcode.Flux.Core.Systems.Workflow.Bases
{
	/// <summary>
	/// Manager class for the exit paths table
	/// </summary>
	public class TaskExitPaths : BaseManager
	{
		#region Constructors
		public TaskExitPaths() : base() { }
		public TaskExitPaths(SqlConnection CurrentConnection) : base(CurrentConnection) { }
		public TaskExitPaths(SqlConnection CurrentConnection, SqlTransaction CurrentTransaction) : base(CurrentConnection, CurrentTransaction) { }
		#endregion
		#region Data Retrieval
		/// <summary>
		/// Get all exit paths associated with a tasking
		/// </summary>
		/// <param name="TaskingID"></param>
		/// <returns></returns>
		public TaskExitPath [] GetExitPathsForTaskingID(int TaskingID)
		{
			DataTable results = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFTaskExitPathsGetByTaskingID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, TaskingID));
			da.Fill(results);
			ReleaseConnection();

			List<TaskExitPath> list = new List<TaskExitPath>();
			foreach (DataRow r in results.Rows)
				list.Add(new TaskExitPath(r));

			return list.ToArray();
		}
		/// <summary>
		/// Gets an exit path by it's own ID
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public TaskExitPath GetTaskExitPathByID(int ID)
		{
			DataTable result = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreWFTaskExitPathsGetByID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			da.Fill(result);
			ReleaseConnection();

			if (result == null) return null;
			if (result.Rows.Count < 1) return null;

			return new TaskExitPath(result.Rows[0]);
		}
		#endregion
		#region Save Records
		/// <summary>
		/// Save the specified exit path to the database
		/// </summary>
		/// <param name="ps"></param>
		/// <returns></returns>
		public int Save(TaskExitPath t)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreWFTaskExitPathsSave");
			SqlParameter result = SQLUtility.ParameterInOut("@id", SqlDbType.Int, 4, t.PK_ID);
			cmd.Parameters.Add(result);

			cmd.Parameters.Add( SQLUtility.ParameterIn("@FK_TaskingID", SqlDbType.Int, 4, t.FK_TaskingID));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@Caption", SqlDbType.NVarChar, 100, t.Caption));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@uiTaskMethod", SqlDbType.NVarChar, 500, t.uiTaskMethod));
			cmd.Parameters.Add( SQLUtility.ParameterIn("@CSV_AllowedGroups", SqlDbType.NVarChar, 500, t.CSV_AllowedGroups));
			cmd.ExecuteNonQuery();
			ReleaseConnection();

			if (t.PK_ID == -1)
				t.PK_ID = (int)result.Value;

			return t.PK_ID;
		}
		#endregion
		#region Delete Records
		/// <summary>
		/// Delete the specified exit path from the database
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public bool DeleteByID(int ID)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreWFTaskExitPathsDeleteByID");
			cmd.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			cmd.ExecuteNonQuery();
			ReleaseConnection();
			return true;
		}
		#endregion
	}
}
